package com.hlingsoft.kkh.action.v20;

import com.hlingsoft.kkh.action.BaseAction;
import com.hlingsoft.kkh.dao.*;
import com.hlingsoft.kkh.entity.*;
import com.hlingsoft.kkh.entity.vo.ShareVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Administrator on 2015/2/15.
 */


@Controller
@RequestMapping("/v20/share")
public class ShareAction_V20 extends BaseAction {




    /**
     * 分享记录
     *
     * @param userId
     * @param imageId
     * @param eventCommentsId
     * @param request
     * @return
     */
    @RequestMapping(value = "/record.json")
    @ResponseBody
    public Map<String, Object> share(
            @RequestParam(value = "userId", defaultValue = "0") long userId,
            @RequestParam(value = "eventsId", required = false) Integer eventsId,
            @RequestParam(value = "eventCommentsId", required = false) Integer eventCommentsId,
            @RequestParam(value = "imageId", required = false) Integer imageId,
            HttpServletRequest request) {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Date now = new Date();
            Share share = new Share();
            share.setMyId(userId);
            share.setCreateTime(now);
            share.setUpdateTime(now);
            if (eventsId != null) {
                Events events = eventsDao.getEventsById(eventsId);
                share.setEventsId(eventsId);
                share.setAppUserId(events.getAppUserId());
            } else if (eventCommentsId != null) {
                EventComments eventComments = eventCommentsDao.getCommentsListById(eventCommentsId);
                share.setEventsId(eventComments.getEventId());
                share.setEventCommentsId(eventCommentsId);
                share.setAppUserId(eventComments.getAppUserId());
            } else if (imageId != null) {
                Image image = imageDao.getImageById(imageId);
                share.setEventsId(image.getEventsId());
                share.setEventCommentsId(image.getEventCommentsId());
                share.setAppUserId(image.getAppUserId());
                share.setImage(image.getPath());
                share.setImageId(imageId);
            }
            shareDao.addShare(share);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }

    /**
     * 分享瞬间
     *
     * @param eventCommentsId
     * @param imageId
     * @param eventCommentsId
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/moment.htm")
    public String moment(
            @RequestParam(value = "imageId") long imageId,     //分享的图片
            @RequestParam(value = "eventCommentsId") long eventCommentsId,
            HttpServletRequest request, ModelMap modelMap) {
        Image image = imageDao.getImageById(imageId);
        EventComments eventComments = eventCommentsDao.getCommentsListById(eventCommentsId);
        AppUser user = userDao.getUserById(eventComments.getAppUserId());
        eventComments.setUserName(user.getName());
        eventComments.setHeadImage(user.getHeadImage());
        List<Image> imageList = imageDao.getImageByEventCommentsId(eventCommentsId);
        eventComments.setImageList(imageList);
        List<Reply> replyList = replyDao.getAllReplyByEventCommentsId(eventCommentsId);
        eventComments.setReplyList(replyList);
        Events events = eventsDao.getEventsById(eventComments.getEventId());
        eventComments.setEventsName(events.getName());
        modelMap.put("image", image);
        modelMap.put("eventComments", eventComments);
        return "system/share/moment";
    }


    /**
     * 图片显示
     *
     * @param eventsId
     * @param request
     * @return
     */
    @RequestMapping(value = "/showImages.json")
    @ResponseBody
    public Map<String, Object> shareEvents(
            @RequestParam(value = "eventsId") long eventsId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Image> imageList = imageDao.getImageByEventsId(eventsId);

            Collections.sort(imageList, new Comparator<Image>() {
                @Override
                public int compare(Image o1, Image o2) {
                    return o1.getTokeTime().compareTo(o2.getTokeTime());
                }
            });

            resultMap.put("imageList", imageList);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;
    }


    /**
     * 分享events
     *
     * @param eventsId
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/events.htm")
    public String shareEvents(
            @RequestParam(value = "eventsId") long eventsId,
            @RequestParam(value = "imageIds") String imageIds,
            HttpServletRequest request, ModelMap modelMap) {

        String[] sourceStrArray = imageIds.split(",");
        //分享的图片
        List<Image> imageList = new ArrayList<Image>();
        for (String string : sourceStrArray) {
            long thelong = Long.valueOf(string);
            Image image = imageDao.getImageById(thelong);
            imageList.add(image);
        }


        List<ShareVo> shareVoList = new ArrayList<ShareVo>();
        //获取events记录
        Events events = eventsDao.getEventsById(eventsId);
        //获取活动发布人信息
        AppUser eventsUser = userDao.getUserById(events.getAppUserId());


        //文字评论
        List<Reply> replyList = replyDao.getReplyByEventsId(eventsId);
        for (Reply reply : replyList) {
            ShareVo shareVo = new ShareVo();
            AppUser user = userDao.getUserById(reply.getMyId());
            shareVo.setHeadImage(user.getHeadImage());
            shareVo.setName(user.getName());
            shareVo.setContent(reply.getContent());
            shareVo.setCreateTime(reply.getUpdateTime());
            shareVoList.add(shareVo);
        }


        modelMap.put("eventsUser", eventsUser);
        modelMap.put("events", events);
        modelMap.put("imageList", imageList);
        modelMap.put("replyList",replyList);
        modelMap.put("shareVoList", shareVoList);

        return "system/share/eventsDetail";
    }


    /**
     * 分享帖子
     *
     * @param eventCommentsId
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/eventComments.htm")
    public String eventComments(
            @RequestParam(value = "eventCommentsId") long eventCommentsId,
            HttpServletRequest request, ModelMap modelMap) {
        EventComments eventComments = eventCommentsDao.getCommentsListById(eventCommentsId);
        Events events = eventsDao.getEventsById(eventComments.getEventId());
        eventComments.setEventsName(events.getName());
        AppUser user = userDao.getUserById(eventComments.getAppUserId());
        eventComments.setUserName(user.getName());
        eventComments.setHeadImage(user.getHeadImage());
        List<Image> imageList = imageDao.getImageByEventCommentsId(eventCommentsId);
        eventComments.setImageList(imageList);
        List<Reply> replyList = replyDao.getAllReplyByEventCommentsId(eventCommentsId);
        eventComments.setReplyList(replyList);
        modelMap.put("eventComments", eventComments);
        return "system/share/eventComments";
    }


}
